package com.pshcp.doctor.api;

import com.framework.base.PageList;
import com.framework.base.ResultBuilder;
import com.framework.base.ResultDTO;
import com.framework.exception.BizException;
import com.framework.logaop.Log;
import com.framework.logaop.LogSourceEnum;
import com.framework.logaop.LogTypeEnum;
import com.framework.thread.ReqComTool;
import com.framework.tool.CommonTool;
import com.pshcp.doctor.ao.sendmessage.DoctorSendMessagePageAO;
import com.pshcp.doctor.ao.sendmessage.SaveDoctorSendMessageAO;
import com.pshcp.doctor.tool.DoctorSendMessageTool;
import com.pshcp.doctor.vo.sendmessage.DoctorSendMessageContentVO;
import com.pshcp.domain.dto.DoctorSendMessageContentDTO;
import com.pshcp.domain.model.DoctorPatient;
import com.pshcp.domain.model.DoctorSendMessageContent;
import com.pshcp.domain.model.DoctorSendMessagePatient;
import com.pshcp.service.DoctorPatientService;
import com.pshcp.service.DoctorSendMessageContentService;
import com.pshcp.service.DoctorSendMessagePatientService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description: TODO
 * @Author: xiaoche
 * @Date: 2022/11/4 01:23
 */
@Api(tags = {"医生端-群发消息接口"})
@Slf4j
@RestController
@RequestMapping("/api/doctor/sendMessage")
public class DoctorSendMessageAPI {

    @Autowired
    private DoctorSendMessageContentService doctorSendMessageContentService;

    @Autowired
    private DoctorSendMessagePatientService doctorSendMessagePatientService;

    @Autowired
    private DoctorPatientService doctorPatientService;

    @ApiOperation("获取群发消息列表(分页)")
    @Log(source = LogSourceEnum.APP, type = LogTypeEnum.SELECT, desc = "获取群发消息列表(分页)")
    @PostMapping("getDoctorSendMessagePage")
    public ResultDTO<PageList<DoctorSendMessageContentVO>> getDoctorSendMessagePage(@RequestBody DoctorSendMessagePageAO doctorSendMessagePageAO) throws BizException {
        DoctorSendMessageContentDTO doctorSendMessageContentDTO = DoctorSendMessageTool.buildGetDoctorSendMessagePage(doctorSendMessagePageAO);
        PageList<DoctorSendMessageContent> pageList = doctorSendMessageContentService.list(doctorSendMessageContentDTO);
        PageList<DoctorSendMessageContentVO> page = CommonTool.buildPageList(pageList, DoctorSendMessageContentVO.class);
        return ResultBuilder.success(page);
    }

    @ApiOperation("保存群发消息")
    @Log(source = LogSourceEnum.APP, type = LogTypeEnum.SELECT, desc = "保存群发消息")
    @PostMapping("saveDoctorSendMessage")
    public ResultDTO<String> saveDoctorSendMessage(@RequestBody SaveDoctorSendMessageAO saveDoctorSendMessageAO) throws BizException {
        DoctorSendMessageTool.checkSaveDoctorSendMessage(saveDoctorSendMessageAO);
        List<DoctorPatient> doctorPatientList = doctorPatientService.getDoctorPatientList(ReqComTool.getUserId(), Arrays.asList(saveDoctorSendMessageAO.getPatientIds()));
        if (CollectionUtils.isEmpty(doctorPatientList)) {
            return ResultBuilder.failure("10130003");
        }
        List<DoctorPatient> doctorPatient2RelationList = doctorPatientList.stream().filter(doctorPatient -> doctorPatient.getRelationFlag() == 1).collect(Collectors.toList());
        if (CollectionUtils.isEmpty(doctorPatient2RelationList) || doctorPatient2RelationList.size() != saveDoctorSendMessageAO.getPatientIds().length) {
            return ResultBuilder.failure("10130003");
        }
        DoctorSendMessageContent saveDoctorSendMessageContent = DoctorSendMessageTool.buildSaveDoctorSendMessageContent(saveDoctorSendMessageAO);
        Long doctorSendMessageContentId = doctorSendMessageContentService.insert(saveDoctorSendMessageContent);
        List<DoctorSendMessagePatient> doctorSendMessagePatients = DoctorSendMessageTool.buildSaveDoctorSendMessagePatientList(doctorSendMessageContentId, saveDoctorSendMessageAO.getPatientIds());
        doctorSendMessagePatientService.batchSaveDoctorSendMessagePatient(doctorSendMessagePatients);
        return ResultBuilder.success(null);
    }
}
